import config from "@/config";
import tool from "@/utils/tool";
import http from "@/utils/request";
// import { useEventSource } from "@vueuse/core";
import sysConfig from "@/config";
// import tool from "@/utils/tool";
export default {
	chat: {
		url: `${config.API_URL}/open/v1/chat/completions`,
		name: "对话",
		post: async function (
			applicationId,
			messageId,
			dialogId,
			data,
			callback,
			success,
			error
		) {
			const request = {
				messageId: messageId,
				messages: data,
				stream: true,
				dialogId: dialogId,
				applicationId: applicationId,
			};
			try {
				let token = tool.cookie.get("TOKEN");
				if (token) {
					token = sysConfig.TOKEN_PREFIX + token;
				}
				let response = await fetch(this.url, {
					method: "post",
					responseType: "stream",
					headers: {
						"Content-Type": "application/json",
						Authorization: token,
					},
					body: JSON.stringify(request),
				});
				// ok字段判断是否成功获取到数据流
				if (!response.ok) {
					success(response);
					return;
				}
				// 用来获取一个可读的流的读取器（Reader）以流的方式处理响应体数据
				const reader = response.body.getReader();
				// 将流中的字节数据解码为文本字符串
				const textDecoder = new TextDecoder();
				let result = true;

				while (result) {
					// done表示流是否已经完成读取  value包含读取到的数据块
					const { done, value } = await reader.read();
					if (done) {
						result = false;
						break;
					}
					// 需要通过decode方法处理数据块，例如转换为文本或进行其他操作
					const chunkText = textDecoder.decode(value);
					callback(chunkText);
				}
			} catch (e) {
				console.log(e);
				error(e);
			}
		},
	},
	imagesGenerate: {
		url: `${config.API_URL}/open/v1/images/generations`,
		name: "文生图",
		post: async function (
			messageId,
			dialogId,
			applicationId,
			data,
			callback,
			success,
			error
		) {
			const request = {
				messageId: messageId,
				message: data,
				dialogId: dialogId,
				applicationId: applicationId,
			};

			try {
				let token = tool.cookie.get("TOKEN");
				if (token) {
					token = sysConfig.TOKEN_PREFIX + token;
				}
				let response = await fetch(this.url, {
					method: "post",
					responseType: "stream",
					headers: {
						"Content-Type": "application/json",
						Authorization: token,
					},
					body: JSON.stringify(request),
				});
				// ok字段判断是否成功获取到数据流
				if (!response.ok) {
					success(response);
					return;
				}
				// 用来获取一个可读的流的读取器（Reader）以流的方式处理响应体数据
				const reader = response.body.getReader();
				// 将流中的字节数据解码为文本字符串
				const textDecoder = new TextDecoder();
				let result = true;

				while (result) {
					// done表示流是否已经完成读取  value包含读取到的数据块
					const { done, value } = await reader.read();
					if (done) {
						result = false;
						break;
					}
					// 需要通过decode方法处理数据块，例如转换为文本或进行其他操作
					const chunkText = textDecoder.decode(value);
					callback(chunkText);
				}
			} catch (e) {
				error(e);
			}
		},
	},
	videosGenerate: {
		url: `${config.API_URL}/open/v1/videos/generations`,
		name: "文生视频",
		post: async function (
			messageId,
			dialogId,
			applicationId,
			data,
			callback,
			success,
			error
		) {
			const request = {
				messageId: messageId,
				message: data,
				dialogId: dialogId,
				applicationId: applicationId,
			};

			try {
				let token = tool.cookie.get("TOKEN");
				if (token) {
					token = sysConfig.TOKEN_PREFIX + token;
				}
				let response = await fetch(this.url, {
					method: "post",
					responseType: "stream",
					headers: {
						"Content-Type": "application/json",
						Authorization: token,
					},
					body: JSON.stringify(request),
				});
				// ok字段判断是否成功获取到数据流
				if (!response.ok) {
					success(response);
					return;
				}
				// 用来获取一个可读的流的读取器（Reader）以流的方式处理响应体数据
				const reader = response.body.getReader();
				// 将流中的字节数据解码为文本字符串
				const textDecoder = new TextDecoder();
				let result = true;

				while (result) {
					// done表示流是否已经完成读取  value包含读取到的数据块
					const { done, value } = await reader.read();
					if (done) {
						result = false;
						break;
					}
					// 需要通过decode方法处理数据块，例如转换为文本或进行其他操作
					const chunkText = textDecoder.decode(value);
					callback(chunkText);
				}
			} catch (e) {
				error(e);
			}
		},
	},
	topicGenerate: {
		url: `${config.API_URL}/open/v1/chat/topic/generate`,
		name: "对话生成摘要主题",
		post: async function (
			applicationId,
			messageId,
			data,
			callback,
			success,
			error
		) {
			const request = {
				messageId: messageId,
				messages: data,
				applicationId: applicationId,
			};

			try {
				let token = tool.cookie.get("TOKEN");
				if (token) {
					token = sysConfig.TOKEN_PREFIX + token;
				}
				let response = await fetch(this.url, {
					method: "post",
					responseType: "stream",
					headers: {
						"Content-Type": "application/json",
						Authorization: token,
					},
					body: JSON.stringify(request),
				});
				// ok字段判断是否成功获取到数据流
				if (!response.ok) {
					success(response);
					return;
				}
				// 用来获取一个可读的流的读取器（Reader）以流的方式处理响应体数据
				const reader = response.body.getReader();
				// 将流中的字节数据解码为文本字符串
				const textDecoder = new TextDecoder();
				let result = true;

				while (result) {
					// done表示流是否已经完成读取  value包含读取到的数据块
					const { done, value } = await reader.read();
					if (done) {
						result = false;
						break;
					}
					// 需要通过decode方法处理数据块，例如转换为文本或进行其他操作
					const chunkText = textDecoder.decode(value);
					callback(chunkText);
				}
			} catch (e) {
				console.log(e);
				error(e);
			}
		},
	},
	dialogCreate: {
		url: `${config.API_URL}/ai/wiki/application/chat/dialog/create`,
		name: "创建会话",
		post: async function (data) {
			return await http.post(this.url, data);
		},
	},
	dialogUpdate: {
		url: `${config.API_URL}/ai/wiki/application/chat/dialog/update`,
		name: "更新会话",
		post: async function (data) {
			return await http.post(this.url, data);
		},
	},
	dialogRemove: {
		url: `${config.API_URL}/ai/wiki/application/chat/dialog/remove`,
		name: "删除会话",
		post: async function (data) {
			return await http.get(this.url + "/" + data);
		},
	},
	dialogList: {
		url: `${config.API_URL}/ai/wiki/application/chat/dialog/list`,
		name: "会话列表",
		post: async function (data, aiType) {
			return await http.get(
				this.url + "?applicationId=" + data + "&aiType=" + aiType
			);
		},
	},
	recordList: {
		url: `${config.API_URL}/ai/wiki/application/chat/record/list`,
		name: "聊天记录列表",
		post: async function (data) {
			return await http.get(this.url + "?dialogId=" + data);
		},
	},
	addRecord: {
		url: `${config.API_URL}/ai/wiki/application/chat/record/create`,
		name: "创建聊天记录",
		post: async function (data) {
			return await http.post(this.url, data);
		},
	},
};
